//
//  SISSReleaseDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/8.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSReleaseDetailViewController.h"
#import "SISSHouseCooperationDetailModel.h"
#import "SISSNetworkingManager+MineCooperation.h"
#import "SISSReceivedRequestViewController.h"
#import "SISSMorePopupView.h"
#import "SISSMyReleaseCooperationViewController.h"

@interface SISSReleaseDetailViewController ()<SISSMorePopupViewDelegate>



@property (weak, nonatomic) IBOutlet UILabel *neighbourhoodLabel;
@property (weak, nonatomic) IBOutlet UILabel *regionAndDetailLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseTypeLabel;
@property (weak, nonatomic) IBOutlet UILabel *areaLabel;
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *listTagLabels;
@property (weak, nonatomic) IBOutlet UILabel *propertyStateLabel;
@property (weak, nonatomic) IBOutlet UILabel *averagePriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *directionLabel;
@property (weak, nonatomic) IBOutlet UILabel *buildYearLabel;
@property (weak, nonatomic) IBOutlet UILabel *decorationLabel;
@property (weak, nonatomic) IBOutlet UILabel *floorLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseCodingLabel;
@property (weak, nonatomic) IBOutlet UILabel *houseDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *releaseTimeLabel;

@property (weak, nonatomic) IBOutlet UILabel *ownerPercentLabel;
@property (weak, nonatomic) IBOutlet UILabel *partnerPercentLabel;
@property (weak, nonatomic) IBOutlet UILabel *cooperationDescriptionLabel;

/* 房源合作详情模型 */
@property (strong, nonatomic) SISSHouseCooperationDetailModel *houseCooperationModel;

@end

@implementation SISSReleaseDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    for (UILabel *label in self.listTagLabels) {
        
        label.hidden = YES;
    }

    // 添加右侧分享删除按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"more..."] style:UIBarButtonItemStylePlain target:self action:@selector(shareAndComplainClick)];
    
}


- (void)setHouseCooperationModel:(SISSHouseCooperationDetailModel *)houseCooperationModel {
    
//    [self.view layoutIfNeeded];
    _houseCooperationModel = houseCooperationModel;
    
    self.releaseTimeLabel.text = houseCooperationModel.cooperation_insert_time;
    self.cooperationDescriptionLabel.text = houseCooperationModel.cooperation_desc;
    
    
    NSArray<UIColor *> *labelCololors = @[COLOR(0x33B189), COLOR(0xF76D81), COLOR(0x8E7FFF), COLOR(0x59ABFD), COLOR(0xE46B00)];
    
    for (int i = 0; i < houseCooperationModel.listTag.count; i++) {
        NSInteger colorIndex = houseCooperationModel.listTag[i].color - 1;
        if (colorIndex < 0) continue; // 保护
        UILabel *label = self.listTagLabels[i];
        label.hidden = NO;
        label.layer.borderWidth = 1;
        label.text = houseCooperationModel.listTag[i].tag;
        label.layer.borderColor = labelCololors[colorIndex].CGColor;
        label.textColor = labelCololors[colorIndex];
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    self.neighbourhoodLabel.text = houseCooperationModel.neighbourhood;
    self.regionAndDetailLabel.text = [NSString stringWithFormat:@"%@-%@",houseCooperationModel.region,houseCooperationModel.regionDetail];
    self.priceLabel.text = [NSString stringWithFormat:@"%0.f",houseCooperationModel.price];
    self.houseTypeLabel.text = houseCooperationModel.houseType;
    self.areaLabel.text = [NSString stringWithFormat:@"%0.f",houseCooperationModel.area];
    self.propertyStateLabel.text = houseCooperationModel.propertyState;
    self.averagePriceLabel.text = [NSString stringWithFormat:@"%.0f元/㎡",houseCooperationModel.averagePrice * 10000];
    self.directionLabel.text = houseCooperationModel.direction.length ? houseCooperationModel.direction: @"暂无数据";
    self.buildYearLabel.text = houseCooperationModel.year ? [NSString stringWithFormat:@"%zd年",houseCooperationModel.year] : @"暂无数据";
    self.decorationLabel.text = houseCooperationModel.decoration.length ? houseCooperationModel.decoration : @"暂无数据";
    self.floorLabel.text = houseCooperationModel.floor > 0 ? [NSString stringWithFormat:@"%zd层",houseCooperationModel.floor] : @"暂无数据";
    self.houseCodingLabel.text = houseCooperationModel.houseInfoNo;
    self.houseDescriptionLabel.text = houseCooperationModel.otherdesc.length ? houseCooperationModel.otherdesc : @"暂无数据";
    
    self.ownerPercentLabel.text = [NSString stringWithFormat:@"%zd%@",houseCooperationModel.commission_rate,@"%"];
    self.partnerPercentLabel.text = [NSString stringWithFormat:@"%zd%@",100 -  houseCooperationModel.commission_rate,@"%"];
}

- (void)setAhcrId:(NSString *)ahcrId {
    _ahcrId = ahcrId;
    
    [self loadData];
}

/**
 分享投诉按钮事件
 */
- (void)shareAndComplainClick {
    
    SISSMorePopupView *moreView = [[SISSMorePopupView alloc] initWithTitles:@[@"分享", @"删除"]];
    moreView.delegate = self;
    [moreView show];
}

#pragma mark - SISSMorePopupViewDelegate
- (void)morePopupViewSelectedWithIndex:(NSInteger)index {
    
    if (index == 0) { // 分享
        NSString *title = [NSString stringWithFormat:@"%@ | %@万",self.houseCooperationModel.neighbourhood, @(self.houseCooperationModel.price)];
        NSString *descr = [NSString stringWithFormat:@"%@-%@ | %@㎡ | %@",self.houseCooperationModel.region,self.houseCooperationModel.regionDetail,@(self.houseCooperationModel.area),self.houseCooperationModel.houseType];
        NSString *webpageUrl = @"";
        if (self.houseCooperationModel.houseresType == 1) { // 线上
            webpageUrl = [NSString stringWithFormat:@"%@/?#!/sellerHouseDetail/%@",WEB_BASE_URL,self.houseCooperationModel.ID];
        } else { // 线下
            webpageUrl = [NSString stringWithFormat:@"%@/#!/offlineHouseDetail/%@",WEB_BASE_URL,self.houseCooperationModel.ID];
        }
        
        [[SISSPublicSingleton shared] shareWithTitle:title descr:descr webpageUrl:webpageUrl currentViewController:self thumImage:self.houseCooperationModel.houseImage.firstObject];
    } else { // 删除
    
        NSString *str = @"删除后不可恢复，您确定删除吗？";
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.alignment = NSTextAlignmentLeft;
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSForegroundColorAttributeName: COLOR(0x334A60), NSParagraphStyleAttributeName: paragraphStyle}];
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"删除" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [alertC setValue:attr forKey:@"attributedMessage"];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"取消");
        }];
        [alertC addAction:cancelAction];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"确定");
            
            [self deleteClickAction];
        }];
        [alertC addAction:okAction];
        
        [self presentViewController:alertC animated:YES completion:nil];
        
    }
    
}



/**
 加载数据
 */
- (void)loadData {
    
    [kNetworkManager requestMyReleaseHouseCooperationDetailWithCooperationId:self.ahcrId success:^(SISSHouseCooperationDetailModel *houseCooperationDetailModel) {
        
        self.houseCooperationModel = houseCooperationDetailModel;
        
//        [self setHouseCooperationModel:houseCooperationDetailModel];
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}



/**
 删除
 */
- (void)deleteClickAction {
    
    [kNetworkManager deleteHouseCooperationWithHouseAhcrId:self.ahcrId successBlock:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) { // 删除成功
            
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [SISSTools showSVTitle:@"删除成功！" andTime:1.0 andColor:YES];
            }];
            if (self.deleteComplete) {
                self.deleteComplete();
            }
            for (UIViewController *controller in self.navigationController.viewControllers) {
                if ([controller isKindOfClass:[SISSMyReleaseCooperationViewController class]]) {
                    [self.navigationController popToViewController:controller animated:YES];
                }
            }
            
        }
        
        
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"删除" message:@"删除后不可恢复,您确定删除吗？" preferredStyle:UIAlertControllerStyleAlert];
//    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
//    
//    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        
//        
//        NSLog(@"删除");
//    }];
//    
//    [alertController addAction:cancelAction];
//    [alertController addAction:confirmAction];
//    
//    [self presentViewController:alertController animated:YES completion:nil];
}




@end
